<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- Copyright © 1988-2023 Free Software Foundation, Inc.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with no
Invariant Sections, the Front-Cover texts being (a) (see below), and
with the Back-Cover Texts being (b) (see below).  A copy of the
license is included in the section entitled "GNU
Free Documentation License".

(a) The FSF's Front-Cover Text is:

A GNU Manual

(b) The FSF's Back-Cover Text is:

You have freedom to copy and modify this GNU Manual, like GNU
     software.  Copies published by the Free Software Foundation raise
     funds for GNU development. -->
<title>Installing GCC: Building</title>

<meta name="description" content="Installing GCC: Building">
<meta name="keywords" content="Installing GCC: Building">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">

<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
ul.mark-bullet {list-style-type: disc}
-->
</style>


</head>

<body lang="en">























<a class="index-entry-id" id="index-Installing-GCC_003a-Building"></a>

<p>Now that GCC is configured, you are ready to build the compiler and
runtime libraries.
</p>
<p>Some commands executed when making the compiler may fail (return a
nonzero status) and be ignored by <code class="command">make</code>.  These failures, which
are often due to files that were not found, are expected, and can safely
be ignored.
</p>
<p>It is normal to have compiler warnings when compiling certain files.
Unless you are a GCC developer, you can generally ignore these warnings
unless they cause compilation to fail.  Developers should attempt to fix
any warnings encountered, however they can temporarily continue past
warnings-as-errors by specifying the configure flag
<samp class="option">--disable-werror</samp>.
</p>
<p>On certain old systems, defining certain environment variables such as
<code class="env">CC</code> can interfere with the functioning of <code class="command">make</code>.
</p>
<p>If you encounter seemingly strange errors when trying to build the
compiler in a directory other than the source directory, it could be
because you have previously configured the compiler in the source
directory.  Make sure you have done all the necessary preparations.
</p>
<p>If you build GCC on a BSD system using a directory stored in an old System
V file system, problems may occur in running <code class="command">fixincludes</code> if the
System V file system doesn&rsquo;t support symbolic links.  These problems
result in a failure to fix the declaration of <code class="code">size_t</code> in
<samp class="file">sys/types.h</samp>.  If you find that <code class="code">size_t</code> is a signed type and
that type mismatches occur, this could be the cause.
</p>
<p>The solution is not to use such a directory for building GCC.
</p>
<p>Similarly, when building from the source repository or snapshots, or if you modify
<samp class="file">*.l</samp> files, you need the Flex lexical analyzer generator
installed.  If you do not modify <samp class="file">*.l</samp> files, releases contain
the Flex-generated files and you do not need Flex installed to build
them.  There is still one Flex-based lexical analyzer (part of the
build machinery, not of GCC itself) that is used even if you only
build the C front end.
</p>
<p>When building from the source repository or snapshots, or if you modify Texinfo
documentation, you need version 4.7 or later of Texinfo installed if you
want Info documentation to be regenerated.  Releases contain Info
documentation pre-built for the unmodified documentation in the release.
</p>
<div class="section-level-extent" id="Building-a-native-compiler">
<h3 class="section"><span>Building a native compiler<a class="copiable-link" href="#Building-a-native-compiler"> &para;</a></span></h3>

<p>For a native build, the default configuration is to perform
a 3-stage bootstrap of the compiler when &lsquo;<samp class="samp">make</samp>&rsquo; is invoked.
This will build the entire GCC system and ensure that it compiles
itself correctly.  It can be disabled with the <samp class="option">--disable-bootstrap</samp>
parameter to &lsquo;<samp class="samp">configure</samp>&rsquo;, but bootstrapping is suggested because
the compiler will be tested more completely and could also have
better performance.
</p>
<p>The bootstrapping process will complete the following steps:
</p>
<ul class="itemize mark-bullet">
<li>Build tools necessary to build the compiler.

</li><li>Perform a 3-stage bootstrap of the compiler.  This includes building
three times the target tools for use by the compiler such as binutils
(bfd, binutils, gas, gprof, ld, and opcodes) if they have been
individually linked or moved into the top level GCC source tree before
configuring.

</li><li>Perform a comparison test of the stage2 and stage3 compilers.

</li><li>Build runtime libraries using the stage3 compiler from the previous step.

</li></ul>

<p>If you are short on disk space you might consider &lsquo;<samp class="samp">make
bootstrap-lean</samp>&rsquo; instead.  The sequence of compilation is the
same described above, but object files from the stage1 and
stage2 of the 3-stage bootstrap of the compiler are deleted as
soon as they are no longer needed.
</p>
<p>If you wish to use non-default GCC flags when compiling the stage2
and stage3 compilers, set <code class="code">BOOT_CFLAGS</code> on the command line when
doing &lsquo;<samp class="samp">make</samp>&rsquo;.  For example, if you want to save additional space
during the bootstrap and in the final installation as well, you can
build the compiler binaries without debugging information as in the
following example.  This will save roughly 40% of disk space both for
the bootstrap and the final installation.  (Libraries will still contain
debugging information.)
</p>
<div class="example smallexample">
<pre class="example-preformatted">make BOOT_CFLAGS='-O' bootstrap
</pre></div>

<p>You can place non-default optimization flags into <code class="code">BOOT_CFLAGS</code>; they
are less well tested here than the default of &lsquo;<samp class="samp">-g -O2</samp>&rsquo;, but should
still work.  In a few cases, you may find that you need to specify special
flags such as <samp class="option">-msoft-float</samp> here to complete the bootstrap; or,
if the native compiler miscompiles the stage1 compiler, you may need
to work around this, by choosing <code class="code">BOOT_CFLAGS</code> to avoid the parts
of the stage1 compiler that were miscompiled, or by using &lsquo;<samp class="samp">make
bootstrap4</samp>&rsquo; to increase the number of stages of bootstrap.
</p>
<p><code class="code">BOOT_CFLAGS</code> does not apply to bootstrapped target libraries.
Since these are always compiled with the compiler currently being
bootstrapped, you can use <code class="code">CFLAGS_FOR_TARGET</code> to modify their
compilation flags, as for non-bootstrapped target libraries.
Again, if the native compiler miscompiles the stage1 compiler, you may
need to work around this by avoiding non-working parts of the stage1
compiler.  Use <code class="code">STAGE1_TFLAGS</code> to this end.
</p>
<p>If you used the flag <samp class="option">--enable-languages=&hellip;</samp> to restrict
the compilers to be built, only those you&rsquo;ve actually enabled will be
built.  This will of course only build those runtime libraries, for
which the particular compiler has been built.  Please note,
that re-defining <code class="env">LANGUAGES</code> when calling &lsquo;<samp class="samp">make</samp>&rsquo;
<strong class="strong">does not</strong> work anymore!
</p>
<p>If the comparison of stage2 and stage3 fails, this normally indicates
that the stage2 compiler has compiled GCC incorrectly, and is therefore
a potentially serious bug which you should investigate and report.  (On
a few systems, meaningful comparison of object files is impossible; they
always appear &ldquo;different&rdquo;.  If you encounter this problem, you will
need to disable comparison in the <samp class="file">Makefile</samp>.)
</p>
<p>If you do not want to bootstrap your compiler, you can configure with
<samp class="option">--disable-bootstrap</samp>.  In particular cases, you may want to
bootstrap your compiler even if the target system is not the same as
the one you are building on: for example, you could build a
<code class="code">powerpc-unknown-linux-gnu</code> toolchain on a
<code class="code">powerpc64-unknown-linux-gnu</code> host.  In this case, pass
<samp class="option">--enable-bootstrap</samp> to the configure script.
</p>
<p><code class="code">BUILD_CONFIG</code> can be used to bring in additional customization
to the build.  It can be set to a whitespace-separated list of names.
For each such <code class="code">NAME</code>, top-level <samp class="file">config/<code class="code">NAME</code>.mk</samp> will
be included by the top-level <samp class="file">Makefile</samp>, bringing in any settings
it contains.  The default <code class="code">BUILD_CONFIG</code> can be set using the
configure option <samp class="option">--with-build-config=<code class="code">NAME</code>...</samp>.  Some
examples of supported build configurations are:
</p>
<dl class="table">
<dt>&lsquo;<samp class="samp">bootstrap-O1</samp>&rsquo;</dt>
<dd><p>Removes any <samp class="option">-O</samp>-started option from <code class="code">BOOT_CFLAGS</code>, and adds
<samp class="option">-O1</samp> to it.  &lsquo;<samp class="samp">BUILD_CONFIG=bootstrap-O1</samp>&rsquo; is equivalent to
&lsquo;<samp class="samp">BOOT_CFLAGS='-g -O1'</samp>&rsquo;.
</p>
</dd>
<dt>&lsquo;<samp class="samp">bootstrap-O3</samp>&rsquo;</dt>
<dt>&lsquo;<samp class="samp">bootstrap-Og</samp>&rsquo;</dt>
<dd><p>Analogous to <code class="code">bootstrap-O1</code>.
</p>
</dd>
<dt>&lsquo;<samp class="samp">bootstrap-lto</samp>&rsquo;</dt>
<dd><p>Enables Link-Time Optimization for host tools during bootstrapping.
&lsquo;<samp class="samp">BUILD_CONFIG=bootstrap-lto</samp>&rsquo; is equivalent to adding
<samp class="option">-flto</samp> to &lsquo;<samp class="samp">BOOT_CFLAGS</samp>&rsquo;.  This option assumes that the host
supports the linker plugin (e.g. GNU ld version 2.21 or later or GNU gold
version 2.21 or later).
</p>
</dd>
<dt>&lsquo;<samp class="samp">bootstrap-lto-noplugin</samp>&rsquo;</dt>
<dd><p>This option is similar to <code class="code">bootstrap-lto</code>, but is intended for
hosts that do not support the linker plugin.  Without the linker plugin 
static libraries are not compiled with link-time optimizations.  Since 
the GCC middle end and back end are in <samp class="file">libbackend.a</samp> this means
that only the front end is actually LTO optimized.
</p>
</dd>
<dt>&lsquo;<samp class="samp">bootstrap-lto-lean</samp>&rsquo;</dt>
<dd><p>This option is similar to <code class="code">bootstrap-lto</code>, but is intended for
faster build by only using LTO in the final bootstrap stage.
With &lsquo;<samp class="samp">make profiledbootstrap</samp>&rsquo; the LTO frontend
is trained only on generator files.
</p>
</dd>
<dt>&lsquo;<samp class="samp">bootstrap-debug</samp>&rsquo;</dt>
<dd><p>Verifies that the compiler generates the same executable code, whether
or not it is asked to emit debug information.  To this end, this
option builds stage2 host programs without debug information, and uses
<samp class="file">contrib/compare-debug</samp> to compare them with the stripped stage3
object files.  If <code class="code">BOOT_CFLAGS</code> is overridden so as to not enable
debug information, stage2 will have it, and stage3 won&rsquo;t.  This option
is enabled by default when GCC bootstrapping is enabled, if
<code class="code">strip</code> can turn object files compiled with and without debug
info into identical object files.  In addition to better test
coverage, this option makes default bootstraps faster and leaner.
</p>
</dd>
<dt>&lsquo;<samp class="samp">bootstrap-debug-big</samp>&rsquo;</dt>
<dd><p>Rather than comparing stripped object files, as in
<code class="code">bootstrap-debug</code>, this option saves internal compiler dumps
during stage2 and stage3 and compares them as well, which helps catch
additional potential problems, but at a great cost in terms of disk
space.  It can be specified in addition to &lsquo;<samp class="samp">bootstrap-debug</samp>&rsquo;.
</p>
</dd>
<dt>&lsquo;<samp class="samp">bootstrap-debug-lean</samp>&rsquo;</dt>
<dd><p>This option saves disk space compared with <code class="code">bootstrap-debug-big</code>,
but at the expense of some recompilation.  Instead of saving the dumps
of stage2 and stage3 until the final compare, it uses
<samp class="option">-fcompare-debug</samp> to generate, compare and remove the dumps
during stage3, repeating the compilation that already took place in
stage2, whose dumps were not saved.
</p>
</dd>
<dt>&lsquo;<samp class="samp">bootstrap-debug-lib</samp>&rsquo;</dt>
<dd><p>This option tests executable code invariance over debug information
generation on target libraries, just like <code class="code">bootstrap-debug-lean</code>
tests it on host programs.  It builds stage3 libraries with
<samp class="option">-fcompare-debug</samp>, and it can be used along with any of the
<code class="code">bootstrap-debug</code> options above.
</p>
<p>There aren&rsquo;t <code class="code">-lean</code> or <code class="code">-big</code> counterparts to this option
because most libraries are only build in stage3, so bootstrap compares
would not get significant coverage.  Moreover, the few libraries built
in stage2 are used in stage3 host programs, so we wouldn&rsquo;t want to
compile stage2 libraries with different options for comparison purposes.
</p>
</dd>
<dt>&lsquo;<samp class="samp">bootstrap-debug-ckovw</samp>&rsquo;</dt>
<dd><p>Arranges for error messages to be issued if the compiler built on any
stage is run without the option <samp class="option">-fcompare-debug</samp>.  This is
useful to verify the full <samp class="option">-fcompare-debug</samp> testing coverage.  It
must be used along with <code class="code">bootstrap-debug-lean</code> and
<code class="code">bootstrap-debug-lib</code>.
</p>
</dd>
<dt>&lsquo;<samp class="samp">bootstrap-cet</samp>&rsquo;</dt>
<dd><p>This option enables Intel CET for host tools during bootstrapping.
&lsquo;<samp class="samp">BUILD_CONFIG=bootstrap-cet</samp>&rsquo; is equivalent to adding
<samp class="option">-fcf-protection</samp> to &lsquo;<samp class="samp">BOOT_CFLAGS</samp>&rsquo;.  This option
assumes that the host supports Intel CET (e.g. GNU assembler version
2.30 or later).
</p>
</dd>
<dt>&lsquo;<samp class="samp">bootstrap-time</samp>&rsquo;</dt>
<dd><p>Arranges for the run time of each program started by the GCC driver,
built in any stage, to be logged to <samp class="file">time.log</samp>, in the top level of
the build tree.
</p>
</dd>
<dt>&lsquo;<samp class="samp">bootstrap-asan</samp>&rsquo;</dt>
<dd><p>Compiles GCC itself using Address Sanitization in order to catch invalid memory
accesses within the GCC code.
</p>
</dd>
<dt>&lsquo;<samp class="samp">bootstrap-hwasan</samp>&rsquo;</dt>
<dd><p>Compiles GCC itself using HWAddress Sanitization in order to catch invalid
memory accesses within the GCC code.  This option is only available on AArch64
systems that are running Linux kernel version 5.4 or later.
</p>
</dd>
</dl>

</div>
<div class="section-level-extent" id="Building-a-cross-compiler">
<h3 class="section"><span>Building a cross compiler<a class="copiable-link" href="#Building-a-cross-compiler"> &para;</a></span></h3>

<p>When building a cross compiler, it is not generally possible to do a
3-stage bootstrap of the compiler.  This makes for an interesting problem
as parts of GCC can only be built with GCC.
</p>
<p>To build a cross compiler, we recommend first building and installing a
native compiler.  You can then use the native GCC compiler to build the
cross compiler.  The installed native compiler needs to be GCC version
2.95 or later.
</p>
<p>Assuming you have already installed a native copy of GCC and configured
your cross compiler, issue the command <code class="command">make</code>, which performs the
following steps:
</p>
<ul class="itemize mark-bullet">
<li>Build host tools necessary to build the compiler.

</li><li>Build target tools for use by the compiler such as binutils (bfd,
binutils, gas, gprof, ld, and opcodes)
if they have been individually linked or moved into the top level GCC source
tree before configuring.

</li><li>Build the compiler (single stage only).

</li><li>Build runtime libraries using the compiler from the previous step.
</li></ul>

<p>Note that if an error occurs in any step the make process will exit.
</p>
<p>If you are not building GNU binutils in the same source tree as GCC,
you will need a cross-assembler and cross-linker installed before
configuring GCC.  Put them in the directory
<samp class="file"><var class="var">prefix</var>/<var class="var">target</var>/bin</samp>.  Here is a table of the tools
you should put in this directory:
</p>
<dl class="table">
<dt><samp class="file">as</samp></dt>
<dd><p>This should be the cross-assembler.
</p>
</dd>
<dt><samp class="file">ld</samp></dt>
<dd><p>This should be the cross-linker.
</p>
</dd>
<dt><samp class="file">ar</samp></dt>
<dd><p>This should be the cross-archiver: a program which can manipulate
archive files (linker libraries) in the target machine&rsquo;s format.
</p>
</dd>
<dt><samp class="file">ranlib</samp></dt>
<dd><p>This should be a program to construct a symbol table in an archive file.
</p></dd>
</dl>

<p>The installation of GCC will find these programs in that directory,
and copy or link them to the proper place to for the cross-compiler to
find them when run later.
</p>
<p>The easiest way to provide these files is to build the Binutils package.
Configure it with the same <samp class="option">--host</samp> and <samp class="option">--target</samp>
options that you use for configuring GCC, then build and install
them.  They install their executables automatically into the proper
directory.  Alas, they do not support all the targets that GCC
supports.
</p>
<p>If you are not building a C library in the same source tree as GCC,
you should also provide the target libraries and headers before
configuring GCC, specifying the directories with
<samp class="option">--with-sysroot</samp> or <samp class="option">--with-headers</samp> and
<samp class="option">--with-libs</samp>.  Many targets also require &ldquo;start files&rdquo; such
as <samp class="file">crt0.o</samp> and
<samp class="file">crtn.o</samp> which are linked into each executable.  There may be several
alternatives for <samp class="file">crt0.o</samp>, for use with profiling or other
compilation options.  Check your target&rsquo;s definition of
<code class="code">STARTFILE_SPEC</code> to find out what start files it uses.
</p>
</div>
<div class="section-level-extent" id="Building-in-parallel">
<h3 class="section"><span>Building in parallel<a class="copiable-link" href="#Building-in-parallel"> &para;</a></span></h3>

<p>GNU Make 3.80 and above, which is necessary to build GCC, support
building in parallel.  To activate this, you can use &lsquo;<samp class="samp">make -j 2</samp>&rsquo;
instead of &lsquo;<samp class="samp">make</samp>&rsquo;.  You can also specify a bigger number, and
in most cases using a value greater than the number of processors in
your machine will result in fewer and shorter I/O latency hits, thus
improving overall throughput; this is especially true for slow drives
and network filesystems.
</p>
</div>
<div class="section-level-extent" id="Building-the-Ada-compiler">
<h3 class="section"><span>Building the Ada compiler<a class="copiable-link" href="#Building-the-Ada-compiler"> &para;</a></span></h3>

<p><a class="uref" href="prerequisites.html#GNAT-prerequisite">GNAT prerequisites</a>.
</p>
</div>
<div class="section-level-extent" id="Building-the-D-compiler">
<h3 class="section"><span>Building the D compiler<a class="copiable-link" href="#Building-the-D-compiler"> &para;</a></span></h3>

<p><a class="uref" href="prerequisites.html#GDC-prerequisite">GDC prerequisites</a>.
</p>
</div>
<div class="section-level-extent" id="Building-with-profile-feedback">
<h3 class="section"><span>Building with profile feedback<a class="copiable-link" href="#Building-with-profile-feedback"> &para;</a></span></h3>

<p>It is possible to use profile feedback to optimize the compiler itself.  This
should result in a faster compiler binary.  Experiments done on x86 using gcc
3.3 showed approximately 7 percent speedup on compiling C programs.  To
bootstrap the compiler with profile feedback, use <code class="code">make profiledbootstrap</code>.
</p>
<p>When &lsquo;<samp class="samp">make profiledbootstrap</samp>&rsquo; is run, it will first build a <code class="code">stage1</code>
compiler.  This compiler is used to build a <code class="code">stageprofile</code> compiler
instrumented to collect execution counts of instruction and branch
probabilities.  Training run is done by building <code class="code">stagetrain</code>
compiler.  Finally a <code class="code">stagefeedback</code> compiler is built
using the information collected.
</p>
<p>Unlike standard bootstrap, several additional restrictions apply.  The
compiler used to build <code class="code">stage1</code> needs to support a 64-bit integral type.
It is recommended to only use GCC for this.
</p>
<p>On Linux/x86_64 hosts with some restrictions (no virtualization) it is
also possible to do autofdo build with &lsquo;<samp class="samp">make
autoprofiledback</samp>&rsquo;. This uses Linux perf to sample branches in the
binary and then rebuild it with feedback derived from the profile.
Linux perf and the <code class="code">autofdo</code> toolkit needs to be installed for
this.
</p>
<p>Only the profile from the current build is used, so when an error
occurs it is recommended to clean before restarting. Otherwise
the code quality may be much worse.
</p>
<hr />
<p>
<p><a class="uref" href="./index.html">Return to the GCC Installation page</a>
</p>






</div>



</body>
</html>
